using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;
using System.Data;
using ShareMarking.Entity;

namespace ShareMarking.Service
{
    public class nc_ncuserService : BaseDao<nc_ncuser>
    {
        #region "单例"
        private static nc_ncuserService service;
        public static nc_ncuserService Instance
        {
            get
            {
                if (service == null)
                {
                    service = new nc_ncuserService();
                }
                return service;
            }
        }
        #endregion


        #region 分页方法
        /// <summary>
        /// NC招募码
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="sort"></param>
        /// <param name="totalCount"></param>
        /// <param name="nc_code"></param>
        /// <param name="nc_name"></param>
        /// <param name="nc_phone"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public DataTable Get_nc_ncuser_PageSelect(int pageNo, int pageSize,
                     string sort, out int totalCount, string nc_code,string nc_name, string nc_phone,string state )
        {
            WhereClip whereClip = new WhereClip("");

            //like
            if (whereClip.ToString().Length > 0)
            {
                //whereClip = WhereClip.Bracket(whereClip);//新版框架这句已经可以不要nc_code, nc_name, nc_phone,state
            }
            if (!nc_name.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.name.Contains(nc_name);
            }
            if (!nc_code.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.nc_code.Contains(nc_code);
            }
            if (!nc_phone.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.tel.Contains(nc_phone);
            }
            if (!state.IsNullOrEmpty())
            {
                if (state == "true")
                {
                    whereClip = whereClip && nc_ncuser._.state == "活跃员工";
                }
                else
                {
                    whereClip = whereClip && nc_ncuser._.state == "离职员工";
                }              
            } 
            return Get_Entity_byPage_ToTable(pageNo, pageSize, whereClip, new OrderByClip(sort), out totalCount);


        }

        /// <summary>
        /// NC妈妈班二维码
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="sort"></param>
        /// <param name="totalCount"></param>
        /// <param name="nc_code"></param>
        /// <param name="nc_name"></param>
        /// <param name="nc_phone"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public DataTable Get_nc_MM_PageSelect(int pageNo, int pageSize,
                    string sort, out int totalCount, string nc_code, string nc_name, string nc_phone, string state)
        {
            WhereClip whereClip = new WhereClip("");

            //like
            if (whereClip.ToString().Length > 0)
            {
                //whereClip = WhereClip.Bracket(whereClip);//新版框架这句已经可以不要nc_code, nc_name, nc_phone,state
            }
            if (!nc_name.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.name.Contains(nc_name);
            }
            if (!nc_code.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.nc_code.Contains(nc_code);
            }
            if (!nc_phone.IsNullOrEmpty())
            {
                whereClip = whereClip && nc_ncuser._.tel.Contains(nc_phone);
            }
            if (!state.IsNullOrEmpty())
            {
                if (state == "true")
                {
                    whereClip = whereClip && nc_ncuser._.state == "活跃员工";
                    whereClip = whereClip && qr_QrCodeRelate._.IsDelete == false;
                }
                else
                {
                    whereClip = whereClip && nc_ncuser._.state == "离职员工";
                    whereClip = whereClip && qr_QrCodeRelate._.IsDelete == true;
                }
            }
            //NCMM标识
            whereClip = whereClip && qr_QrCodeRelate._.qr_Type == 1006;
            

            totalCount = db.From<nc_ncuser>()
                   .LeftJoin<qr_QrCodeRelate>(nc_ncuser._.nc_code == qr_QrCodeRelate._.rel_Code)
                   .LeftJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                   .Where(whereClip)
                   .Count();
            //数据
            return db.From<nc_ncuser>()
                    .LeftJoin<qr_QrCodeRelate>(nc_ncuser._.nc_code == qr_QrCodeRelate._.rel_Code)
                    .LeftJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                    .Where(whereClip)
                    .Select(nc_ncuser._.nc_code, nc_ncuser._.name, nc_ncuser._.nc_nature,
                    nc_ncuser._.ncl_code, nc_ncuser._.entr_date, nc_ncuser._.leave_date,
                    nc_ncuser._.tel, nc_ncuser._.state, qr_QrCode._.qrcode_url)
                    .OrderBy(new OrderByClip(sort))
                    .GetPage(pageSize)
                    .ToTable(pageNo);

        }
        #endregion

    }

}

